package ru.space_game.entities;

import ru.space_game.core.Game;
import ru.space_game.resource.Sounds;

public class Weapon {

	/** The time at which last fired a shot */
	private long lastFire;

	/** The interval between our players shot (ms) */
	private long firingInterval = 200;

	/** List of shots */
	private ShotEntity[] shots;

	/** Last shot index */
	private int shotIndex;

	public Weapon() {
		// setup shots, must be greater than max possible amount on the screen
		shots = new ShotEntity[10];
		for (int i = 0; i < shots.length; i++) {
			shots[i] = new ShotEntity(Game.getInstance(), "shot.gif", 0, 0);
		}
	}

	public boolean canFire() {
		// check that we have waiting long enough to fire
		if (System.currentTimeMillis() - lastFire < firingInterval) {
			return false;
		}
		// if we waited long enough record the time
		lastFire = System.currentTimeMillis();
		return true;
	}

	public void fire(Entity entity) {
		ShotEntity shot = shots[shotIndex++ % shots.length];
		shot.reinitialize((int) entity.getX(), (int) entity.getY());
		Game.getInstance().getWorld().getAddList().add(shot);
		if (entity.isVisible())
			Game.getInstance().getSoundManager().playEffect(Sounds.SOUND_SHOT);
	}
}
